//
//  SiftViewController.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/1.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "SiftViewController.h"
#import "CollectionViewCell.h"
#import "NetworkHandle.h"
#import "SiftBaseModel.h"
@interface SiftViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)UICollectionView *collectionV;
@property(nonatomic, retain)UIScrollView *scrView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation SiftViewController

-(void)dealloc
{
    [_collectionV release];
    [_scrView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.dataArr = [NSMutableArray array];
    self.navigationItem.title = @"案例筛选";
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBtnAction)] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBtnAction)] autorelease];
    self.scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49)];
    _scrView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    _scrView.alwaysBounceVertical = YES;
    [self.view addSubview:_scrView];
    [_scrView release];
    
    [self createLabel];
    [self createCollectionView];
    [self dataHandle];
}

-(void)createLabel
{
    UILabel *areaLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    areaLable.text = @"面积";
    areaLable.textAlignment = 1;
    areaLable.font = [UIFont systemFontOfSize:22];
    [_scrView addSubview:areaLable];
    [areaLable release];
    
    UILabel *costLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, SCREEN_WIDTH, 50)];
    costLable.text = @"预算";
    costLable.textAlignment = 1;
    costLable.font = [UIFont systemFontOfSize:22];
    [_scrView addSubview:costLable];
    [costLable release];
    UILabel *typeLable = [[UILabel alloc] initWithFrame:CGRectMake(0,300, SCREEN_WIDTH, 50)];
    typeLable.text = @"户型";
    typeLable.textAlignment = 1;
    typeLable.font = [UIFont systemFontOfSize:22];
    [_scrView addSubview:typeLable];
    [typeLable release];
    UILabel *styleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 450, SCREEN_WIDTH, 50)];
    styleLable.text = @"风格";
    styleLable.textAlignment = 1;
    styleLable.font = [UIFont systemFontOfSize:22];
    [_scrView addSubview:styleLable];
    [styleLable release];
    
}
-(void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 20;
    //layout.minimumInteritemSpacing = 30;
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.itemSize = CGSizeMake((SCREEN_WIDTH-40-40) /3, 160);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 500, SCREEN_WIDTH, 200) collectionViewLayout:layout];
    _collectionV.backgroundColor = [UIColor whiteColor];
    _collectionV.dataSource = self;
    _collectionV.delegate = self;
    [_scrView addSubview:_collectionV];
    [layout release];
    [_collectionV release];
    [_collectionV registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"SiftVCCell"];
}

-(void)dataHandle
{
    
    NSString *str = @"http://api.guju.com.cn/v2/photo/map";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.dataArr = [SiftBaseModel transformWithArray:responseObject[@"style"]];
        [self.dataArr removeObjectAtIndex:0];
        [_collectionV reloadData];
    } filed:^(NSError *error) {
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SiftVCCell" forIndexPath:indexPath];
    SiftBaseModel *model = _dataArr[indexPath.item];
    NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w200_h120_m0.jpg",model.mId];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    cell.titleLabel.text = model.name;
    return cell;
}
/**
 *  导航栏左侧按钮
 */
-(void)leftBtnAction
{
    
}
/**
 *  导航栏右侧按钮
 */
-(void)rightBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
